layui.define(function (exports) {
  let $ = layui.$
  let NoReadCount = 0


  // 判断客户端类型
  let clientType = sessionStorage.getItem('clientType') || 'Online'
  // let clientType = sessionStorage.getItem('clientType') || 'Offline'
  // 菜单
  const menuListOnline = [
    {
      path: 'home',
      icon: 'res/img/indexOff.png',
      iconActived: 'res/img/indexOn.png',
      title: '首页'
    },
    {
      path: 'alarm',
      icon: 'res/img/alarmOff.png',
      iconActived: 'res/img/alarmOn.png',
      title: '告警记录'
    },
    {
      path: 'message',
      icon: 'res/img/messageOff.png',
      iconActived: 'res/img/messageOn.png',
      msgNum: 0,
      title: '消息通知'
    }
  ]
  const menuListOffLine = [
    {
      path: 'home',
      icon: 'res/img/indexOff.png',
      iconActived: 'res/img/indexOn.png',
      title: '首页'
    },
    {
      path: 'selfCheck',
      icon: 'res/img/selfCheckOff.png',
      iconActived: 'res/img/selfCheckOn.png',
      title: '保密自查'
    },
    {
      path: 'alarm',
      icon: 'res/img/alarmOff.png',
      iconActived: 'res/img/alarmOn.png',
      title: '告警记录'
    },
    {
      path: 'strategy',
      icon: 'res/img/strategyOff.png',
      iconActived: 'res/img/strategyOn.png',
      title: '策略配置'
    }
  ]

  let menuList = clientType === 'Online' ? menuListOnline : menuListOffLine


  window.addEventListener("setItemEvent", function (e) {
    if (e.key === 'MainwtMessageNoReadRes') { // 获取未读消息数量
      var _this = sessionStorage.getItem('MainwtMessageNoReadRes')
      // TODO 获取客户端类型
      if (_this != e.newValue) {
        let res = e.newValue ? JSON.parse(e.newValue) : {}
        // 保存客户端类型
        if (res.ClientType) {
          NoReadCount = res.Count || 0
          menuList[2].msgNum = NoReadCount
        }
      }
    } else if (e.key === 'ClientTypeRes') { // 客户端状态
      let clientType = JSON.parse(e.newValue).ClientType || 'Online'
      if (clientType === 'Online') {
        getCurrentMsgNum()
      }
      // 保存客户端类型
      if (clientType) {
        sessionStorage.setItem('clientType', clientType)
        menuList = clientType === 'Online' ? menuListOnline : menuListOffLine
        setMenuListDOM()
      }
    } else if (e.key === 'task_states') {
      let res = JSON.parse(e.newValue) || {}
      let resState = res.scan_state
      // 1, //扫描开始  // 2, //扫描停止 // 3, //扫描中 // 4 //扫描结束
      if (resState * 1 === 1 || resState * 1 === 3) {
        let layer = layui.layer
        sessionStorage.setItem('task_id', res.task_id)
      } else if (resState * 1 === 2 || resState * 1 === 4) {
        sessionStorage.removeItem('task_id')
      }
    }
  });



  // 获取 在线离线 ClientType
  function getClientType() {
    let data = {
      cmd: 'ClientTypeReq'
    }
    reqPageData(data)
  }
  getClientType()

  // 消息数量
  function getMenuMsgNum() {
    let data = {
      cmd: 'MainwtMessageNoReadReq'
    }
    reqPageData(data)
  }

  // 设置消息数字 展示形式
  function setMessageNumber(num) {
    let numContent = ''
    if (num > 0) {
      numContent = num > 99 ? '<div class="number-icon"><div class="number-more">99+</div></div>' : ('<div class="number-icon">' + num + '</div>')
    }
    return numContent
  }
  // 渲染菜单
  function setMenuListDOM() {
    let leftMenuHtml = ''
    let hash = window.location.hash ? window.location.hash.split('#')[1] : ''
    hash = hash.indexOf('?') > -1 ? hash.split('?')[0] : hash
    layui.each(menuList, function (index, item) {
      if (hash === '' && item.path === 'home') {
        leftMenuHtml += '<div class="menu-item actived-this" id="'
          + item.path + '" menu-event="pathTo" data-menu="'
          + item.path + '"><img class="menu-icon" data-imgon='
          + item.iconActived + ' data-imgoff='
          + item.icon + ' src="'
          + item.iconActived + '"><div class="text">'
          + item.title + '</div></div>'
      } else {
        leftMenuHtml += '<div class="menu-item '
          + (hash === item.path ? 'actived-this' : '')
          + '"id="' + item.path + '" menu-event="pathTo" data-menu="'
          + item.path + '"><img class="menu-icon" data-imgon='
          + item.iconActived + ' data-imgoff='
          + item.icon + ' src="'
          + (hash === item.path ? item.iconActived : item.icon) + '">'
          + setMessageNumber(item.msgNum) + '<div class="text">'
          + item.title + '</div></div>'
      }
    })
    $('.menu-list').html(leftMenuHtml);
  }

  function getCurrentMsgNum() {
    // 轮询监听 消息数量
    if (clientType === 'Online') {
      getMenuMsgNum()
      clearInterval(getMsgNum)
      var getMsgNum = setInterval(() => {
        getMenuMsgNum()
        let num = NoReadCount * 1
        num = num > 99 ? '<div class="number-more">99+</div>' : num
        $('.menu-list .menu-item .number-icon').html(num);
      }, 2000)
    }
  }

  // 渲染菜单
  setMenuListDOM()

  // 菜单点击事件
  let util = layui.util
  util.event('menu-event', {
    pathTo: function (param) {
      $('.menu-item.actived-this .menu-icon').attr("src", $('.menu-item.actived-this .menu-icon')[0].dataset.imgoff);
      $('.menu-item.actived-this').removeClass('actived-this')
      $(param).addClass('actived-this')
      $('.menu-item.actived-this .menu-icon').attr("src", $('.menu-item.actived-this .menu-icon')[0].dataset.imgon);
      window.location.hash = param[0].dataset.menu
    }
  })
  // 监听hash路由变化
  $(window).on('hashchange', function () {
    $('.menu-item.actived-this .menu-icon').attr("src", $('.menu-item.actived-this .menu-icon')[0].dataset.imgoff);
    $('.menu-item.actived-this').removeClass('actived-this')
    let hash = window.location.hash
    hash = hash.indexOf('?') > -1 ? hash.split('?')[0] : hash
    $(hash).addClass('actived-this')
    $('.menu-item.actived-this .menu-icon').attr("src", $('.menu-item.actived-this .menu-icon')[0].dataset.imgon);
  })
  exports('minMenu', {}); // 模块输出的核心，模块名必须和 use 时的模块名一致

});